import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

import store from "@/store";

let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;

VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject);
  } else {
    originPush.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};
VueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject);
  } else {
    originReplace.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};

let router = new VueRouter({
  routes: [
    {
      path: "*",
      redirect: "/home",
      meta: { footerShow: true },
    },
    {
      path: "/home",
      component: () => import("@/pages/Home/Home"),
      meta: { footerShow: true },
    },
    {
      path: "/login",
      component: () => import("@/pages/Login/Login"),
      meta: { footerShow: false },
    },
    {
      path: "/register",
      component: () => import("@/pages/Register/Register"),
      meta: { footerShow: false },
    },
    {
      path: "/search/:keyword?",
      component: () => import("@/pages/Search/Search"),
      name: "search",
      meta: { footerShow: true },
    },
    {
      path: "/detail/:skuid?",
      component: () => import("@/pages/Detail/Detail"),
      name: "detail",
    },
    {
      path: "/addcartsuccess",
      component: () => import("@/pages/AddCartSuccess/AddCartSuccess"),
      name: "addcartsuccess",
      meta: { footerShow: true },
    },
    {
      path: "/shopcar",
      component: () => import("@/pages/ShopCar/ShopCar"),
      name: "shopcar",
      meta: { footerShow: true },
    },
    {
      path: "/trade",
      component: () => import("@/pages/Trade/Trade"),
      name: "trade",
      meta: { footerShow: true },
      beforeEnter: (to, from, next) => {
        if (from.path == "/shopcar") {
          next();
        } else if (from.path == "/login") {
          next("/home");
        } else {
          next(false);
        }
      },
    },
    {
      path: "/pay",
      component: () => import("@/pages/Pay/Pay"),
      name: "pay",
      meta: { footerShow: true },
      beforeEnter: (to, from, next) => {
        if (from.path == "/trade") {
          next();
        } else {
          next(false);
        }
      },
    },
    {
      path: "/paysuccess",
      component: () => import("@/pages/PaySuccess/PaySuccess"),
      name: "paysuccess",
      meta: { footerShow: true },
    },
    {
      path: "/center",
      component: () => import("@/pages/Center/Center"),
      name: "center",
      meta: {
        footerShow: true,
      },
      children: [
        {
          path: "/center",
          redirect: "/center/myorder",
        },
        {
          path: "myorder",
          component: () => import("@/pages/Center/MyOrder/MyOrder"),
          name: "myorder",
        },
        {
          path: "grouporder",
          component: () => import("@/pages/Center/GroupOrder/GroupOrder"),
          name: "grouporder",
        },
      ],
    },
    {
      path: "/communication",
      component: () => import("@/pages/Communication/Communication"),
      children: [
        {
          path: "event",
          component: () => import("@/pages/Communication/EventTest/EventTest"),
        },
        {
          path: "model",
          component: () => import("@/pages/Communication/ModelTest/ModelTest"),
        },
        {
          path: "sync",
          component: () => import("@/pages/Communication/SyncTest/SyncTest"),
        },
        {
          path: "attrs-listeners",
          component: () => import("@/pages/Communication/AttrsListeners/AttrsListeners"),
        },
        {
          path: "children-parent",
          component: () => import("@/pages/Communication/ChildrenParent/ChildrenParent"),
        },
      ],
    },
  ],
  scrollBehavior() {
    return {
      y: 0,
    };
  },
});
router.beforeEach(async (to, from, next) => {
  let token = store.state.user.token;
  let userInfo = store.state.user.userInfo.name;
  if (token) {
    if (to.path == "/login") {
      alert("您已经登录过了");
      next("/home");
    } else {
      if (userInfo) {
        next();
      } else {
        try {
          await store.dispatch("getUserInfo");
          next();
        } catch (error) {
          await store.dispatch("userLogOut");
          next("/login");
        }
      }
    }
  } else {
    let toPath = to.path;
    if (toPath == "/trade" || toPath.indexOf("/pay") != -1 || toPath.indexOf("/center") != -1) {
      next("/login?redirect=" + toPath);
    } else {
      next();
    }
  }
});
export default router;
